package com.dietdroid;

import java.util.ArrayList;


import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class AllergenActivity extends Activity {
	
	private ProfileData data;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.allergen);
        
        ListView list = (ListView) findViewById(R.id.allergenList12);
        data = DataStorage.getProfileData(this.getApplicationContext());
        if(data.getAllergens() == null) {
        	list.setAdapter(null);
        }else {
            list.setAdapter(new AllergenAdapter(data.getAllergens()));
        }
        
        //ProfileData data1 = new ProfileData();
       // DataStorage.saveProfileData(this.getApplicationContext(), data1);
        
        Button add = (Button) findViewById(R.id.addAllergen12);
		add.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				buildAddDialog();
			}
		});

	}
  
	private class AllergenAdapter extends ArrayAdapter<String>{
		/*
		 * The data to display in the list.
		 */
		private ArrayList<String> allergen_list;
		
		/**
		 * NutritionHistoryAdapter constructor.
		 * 
		 * @param history_list The data to display.
		 */
		public AllergenAdapter(
				ArrayList<String> allergen_list) {
			super(AllergenActivity.this, R.layout.allergenrow1, allergen_list);
			
			this.allergen_list = allergen_list;
			
		}
		
		/*
		 * (non-Javadoc)
		 * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
		 */
	    @Override
	    public View getView(int position, View convertView, ViewGroup parent) {
	    	View v = convertView;
	    	String model = allergen_list.get(position);
	    	final int p = position;
	    	if (v == null) {
	    		LayoutInflater inflater = ((Activity)AllergenActivity.this).getLayoutInflater();
	    		v = inflater.inflate(R.layout.allergenrow1, parent, false);
		    	TextView bt = (TextView) v.findViewById(R.id.allergen12);
		    	bt.setText(model);
		    	
		        Button remove = (Button) v.findViewById(R.id.deleteAllergen);
				remove.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						data.getAllergens().remove(p);
						DataStorage.saveProfileData(AllergenActivity.this.getApplicationContext(), data);
						ListView list = (ListView) findViewById(R.id.allergenList12);
						list.setAdapter(new AllergenAdapter(data.getAllergens()));
					}
				});
	    	}  	
	    	return v;
	    }
	}
	
    private void buildAddDialog() {
		final Dialog dialog = new Dialog(this);
		dialog.setContentView(R.layout.addallergen);
		dialog.setTitle("Add Allergen");
		dialog.setCancelable(true);

		// set up text
		final EditText edit = (EditText) dialog.findViewById(R.id.editMe);
		// set up button
		Button button = (Button) dialog.findViewById(R.id.add);
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				String allergen = edit.getText().toString();
				data.addAllergen(allergen);
				DataStorage.saveProfileData(AllergenActivity.this.getApplicationContext(), data);
			    ListView allergenList = (ListView) findViewById(R.id.allergenList12);
				allergenList.setAdapter(new AllergenAdapter(data.getAllergens()));
			    dialog.dismiss();
			}
		});
		Button button1 = (Button) dialog.findViewById(R.id.cancel);
		button1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				dialog.dismiss();
			}
		});
		dialog.show(); 
    }
}
